<template>
  <div class="environment-editor-container">
    <n-card class="environment-card">
      <template #header>
        <div class="card-header">
          <h1>环境编辑器</h1>
          <div class="header-actions">
            <n-button @click="saveEnvironment" type="primary">
              <template #icon><n-icon><SaveOutline /></n-icon></template>
              保存环境
            </n-button>
            <n-button @click="goBack">
              <template #icon><n-icon><ArrowBackOutline /></n-icon></template>
              返回
            </n-button>
          </div>
        </div>
      </template>
      
      <div class="content-layout">
        <div class="preview-section">
          <div class="preview-header">
            <h3>环境预览</h3>
            <n-space>
              <n-button-group size="small">
                <n-button @click="setTimeOfDay('dawn')">黎明</n-button>
                <n-button @click="setTimeOfDay('day')">白天</n-button>
                <n-button @click="setTimeOfDay('dusk')">黄昏</n-button>
                <n-button @click="setTimeOfDay('night')">夜晚</n-button>
              </n-button-group>
            </n-space>
          </div>
          
          <div class="preview-container">
            <div class="environment-preview" :class="`time-${currentEnvironment.timeOfDay}`">
              <div class="weather-effect" :class="`weather-${currentEnvironment.weather}`"></div>
              <div class="lighting-effect" :style="lightingStyle"></div>
              <img :src="sceneThumbnail" alt="场景预览" />
            </div>
          </div>
        </div>
        
        <div class="settings-section">
          <n-tabs type="line" animated>
            <n-tab-pane name="lighting" tab="光照设置">
              <div class="tab-content">
                <n-form
                  :model="environmentForm"
                  label-placement="left"
                  label-width="100"
                  require-mark-placement="right-hanging"
                >
                  <n-form-item label="时间" path="timeOfDay">
                    <n-select
                      v-model:value="environmentForm.timeOfDay"
                      :options="timeOfDayOptions"
                    />
                  </n-form-item>
                  
                  <n-form-item label="亮度" path="brightness">
                    <n-slider
                      v-model:value="environmentForm.brightness"
                      :min="0"
                      :max="100"
                      :step="1"
                    />
                  </n-form-item>
                  
                  <n-form-item label="对比度" path="contrast">
                    <n-slider
                      v-model:value="environmentForm.contrast"
                      :min="0"
                      :max="200"
                      :step="1"
                    />
                  </n-form-item>
                  
                  <n-form-item label="饱和度" path="saturation">
                    <n-slider
                      v-model:value="environmentForm.saturation"
                      :min="0"
                      :max="200"
                      :step="1"
                    />
                  </n-form-item>
                  
                  <n-form-item label="色调" path="hue">
                    <n-slider
                      v-model:value="environmentForm.hue"
                      :min="0"
                      :max="360"
                      :step="1"
                    />
                  </n-form-item>
                </n-form>
              </div>
            </n-tab-pane>
            
            <n-tab-pane name="weather" tab="天气效果">
              <div class="tab-content">
                <n-form
                  :model="environmentForm"
                  label-placement="left"
                  label-width="100"
                  require-mark-placement="right-hanging"
                >
                  <n-form-item label="天气类型" path="weather">
                    <n-select
                      v-model:value="environmentForm.weather"
                      :options="weatherOptions"
                    />
                  </n-form-item>
                  
                  <template v-if="environmentForm.weather === 'rain'">
                    <n-form-item label="雨量" path="weatherIntensity">
                      <n-slider
                        v-model:value="environmentForm.weatherIntensity"
                        :min="1"
                        :max="10"
                        :step="1"
                      />
                    </n-form-item>
                  </template>
                  
                  <template v-if="environmentForm.weather === 'snow'">
                    <n-form-item label="雪量" path="weatherIntensity">
                      <n-slider
                        v-model:value="environmentForm.weatherIntensity"
                        :min="1"
                        :max="10"
                        :step="1"
                      />
                    </n-form-item>
                  </template>
                  
                  <template v-if="environmentForm.weather === 'fog'">
                    <n-form-item label="雾气浓度" path="weatherIntensity">
                      <n-slider
                        v-model:value="environmentForm.weatherIntensity"
                        :min="1"
                        :max="10"
                        :step="1"
                      />
                    </n-form-item>
                    <n-form-item label="雾气颜色" path="fogColor">
                      <n-color-picker v-model:value="environmentForm.fogColor" />
                    </n-form-item>
                  </template>
                  
                  <template v-if="environmentForm.weather === 'clouds'">
                    <n-form-item label="云量" path="weatherIntensity">
                      <n-slider
                        v-model:value="environmentForm.weatherIntensity"
                        :min="1"
                        :max="10"
                        :step="1"
                      />
                    </n-form-item>
                    <n-form-item label="云速" path="cloudSpeed">
                      <n-slider
                        v-model:value="environmentForm.cloudSpeed"
                        :min="1"
                        :max="10"
                        :step="1"
                      />
                    </n-form-item>
                  </template>
                </n-form>
              </div>
            </n-tab-pane>
            
            <n-tab-pane name="effects" tab="特殊效果">
              <div class="tab-content">
                <n-form
                  :model="environmentForm"
                  label-placement="left"
                  label-width="100"
                  require-mark-placement="right-hanging"
                >
                  <n-form-item label="滤镜" path="filter">
                    <n-select
                      v-model:value="environmentForm.filter"
                      :options="filterOptions"
                    />
                  </n-form-item>
                  
                  <n-form-item label="滤镜强度" path="filterIntensity">
                    <n-slider
                      v-model:value="environmentForm.filterIntensity"
                      :min="0"
                      :max="100"
                      :step="1"
                    />
                  </n-form-item>
                  
                  <n-divider />
                  
                  <n-form-item label="景深效果">
                    <n-switch v-model:value="environmentForm.depthOfField" />
                  </n-form-item>
                  
                  <template v-if="environmentForm.depthOfField">
                    <n-form-item label="模糊强度" path="blurIntensity">
                      <n-slider
                        v-model:value="environmentForm.blurIntensity"
                        :min="0"
                        :max="20"
                        :step="0.5"
                      />
                    </n-form-item>
                    <n-form-item label="焦点距离" path="focusDistance">
                      <n-slider
                        v-model:value="environmentForm.focusDistance"
                        :min="0"
                        :max="100"
                        :step="1"
                      />
                    </n-form-item>
                  </template>
                  
                  <n-divider />
                  
                  <n-form-item label="光晕效果">
                    <n-switch v-model:value="environmentForm.bloom" />
                  </n-form-item>
                  
                  <template v-if="environmentForm.bloom">
                    <n-form-item label="光晕强度" path="bloomIntensity">
                      <n-slider
                        v-model:value="environmentForm.bloomIntensity"
                        :min="0"
                        :max="100"
                        :step="1"
                      />
                    </n-form-item>
                    <n-form-item label="光晕阈值" path="bloomThreshold">
                      <n-slider
                        v-model:value="environmentForm.bloomThreshold"
                        :min="0"
                        :max="100"
                        :step="1"
                      />
                    </n-form-item>
                  </template>
                </n-form>
              </div>
            </n-tab-pane>
          </n-tabs>
        </div>
      </div>
      
      <n-divider />
      
      <div class="presets-section">
        <h3>环境预设</h3>
        <n-grid :cols="5" :x-gap="12" :y-gap="12">
          <n-grid-item v-for="preset in environmentPresets" :key="preset.id">
            <div
              class="preset-item"
              :class="{ 'selected': selectedPresetId === preset.id }"
              @click="applyPreset(preset)"
            >
              <div class="preset-thumbnail" :style="preset.style">
                <img :src="sceneThumbnail" alt="预设预览" />
              </div>
              <span>{{ preset.name }}</span>
            </div>
          </n-grid-item>
        </n-grid>
      </div>
    </n-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useMessage } from 'naive-ui'
import {
  NCard,
  NButton,
  NButtonGroup,
  NIcon,
  NSpace,
  NTabs,
  NTabPane,
  NForm,
  NFormItem,
  NSelect,
  NSlider,
  NSwitch,
  NColorPicker,
  NDivider,
  NGrid,
  NGridItem
} from 'naive-ui'
import {
  SaveOutline,
  ArrowBackOutline
} from '@vicons/ionicons5'

const router = useRouter()
const message = useMessage()

// 场景缩略图
const sceneThumbnail = ref('https://picsum.photos/id/1015/400/225')

// 选中的预设
const selectedPresetId = ref('')

// 环境表单
const environmentForm = reactive({
  name: '默认环境',
  timeOfDay: 'day',
  brightness: 50,
  contrast: 100,
  saturation: 100,
  hue: 0,
  weather: 'clear',
  weatherIntensity: 5,
  fogColor: '#FFFFFF',
  cloudSpeed: 5,
  filter: 'none',
  filterIntensity: 50,
  depthOfField: false,
  blurIntensity: 5,
  focusDistance: 50,
  bloom: false,
  bloomIntensity: 30,
  bloomThreshold: 50
})

// 当前环境设置
const currentEnvironment = computed(() => ({
  ...environmentForm
}))

// 光照样式
const lightingStyle = computed(() => {
  return {
    filter: `brightness(${environmentForm.brightness / 100}) contrast(${environmentForm.contrast / 100}) saturate(${environmentForm.saturation / 100}) hue-rotate(${environmentForm.hue}deg)`
  }
})

// 时间选项
const timeOfDayOptions = [
  { label: '黎明', value: 'dawn' },
  { label: '白天', value: 'day' },
  { label: '黄昏', value: 'dusk' },
  { label: '夜晚', value: 'night' }
]

// 天气选项
const weatherOptions = [
  { label: '晴朗', value: 'clear' },
  { label: '多云', value: 'clouds' },
  { label: '雨', value: 'rain' },
  { label: '雪', value: 'snow' },
  { label: '雾', value: 'fog' }
]

// 滤镜选项
const filterOptions = [
  { label: '无', value: 'none' },
  { label: '复古', value: 'sepia' },
  { label: '黑白', value: 'grayscale' },
  { label: '冷色调', value: 'cool' },
  { label: '暖色调', value: 'warm' },
  { label: '戏剧性', value: 'dramatic' },
  { label: '梦幻', value: 'dreamy' }
]

// 环境预设
const environmentPresets = [
  {
    id: 'preset1',
    name: '晴朗白天',
    timeOfDay: 'day',
    weather: 'clear',
    brightness: 60,
    contrast: 110,
    saturation: 120,
    style: {
      filter: 'brightness(1.2) contrast(1.1) saturate(1.2)'
    }
  },
  {
    id: 'preset2',
    name: '雨天',
    timeOfDay: 'day',
    weather: 'rain',
    brightness: 40,
    contrast: 105,
    saturation: 80,
    style: {
      filter: 'brightness(0.8) contrast(1.05) saturate(0.8)'
    }
  },
  {
    id: 'preset3',
    name: '黄昏',
    timeOfDay: 'dusk',
    weather: 'clear',
    brightness: 45,
    contrast: 110,
    saturation: 130,
    hue: 15,
    style: {
      filter: 'brightness(0.9) contrast(1.1) saturate(1.3) hue-rotate(15deg)'
    }
  },
  {
    id: 'preset4',
    name: '夜晚',
    timeOfDay: 'night',
    weather: 'clear',
    brightness: 30,
    contrast: 120,
    saturation: 70,
    hue: 240,
    style: {
      filter: 'brightness(0.6) contrast(1.2) saturate(0.7) hue-rotate(240deg)'
    }
  },
  {
    id: 'preset5',
    name: '雾天',
    timeOfDay: 'day',
    weather: 'fog',
    brightness: 55,
    contrast: 90,
    saturation: 70,
    style: {
      filter: 'brightness(1.1) contrast(0.9) saturate(0.7)'
    }
  }
]

// 设置时间
const setTimeOfDay = (time: string) => {
  environmentForm.timeOfDay = time
  
  // 根据时间调整其他参数
  switch (time) {
    case 'dawn':
      environmentForm.brightness = 45
      environmentForm.contrast = 90
      environmentForm.saturation = 80
      environmentForm.hue = 10
      break
    case 'day':
      environmentForm.brightness = 60
      environmentForm.contrast = 100
      environmentForm.saturation = 100
      environmentForm.hue = 0
      break
    case 'dusk':
      environmentForm.brightness = 45
      environmentForm.contrast = 110
      environmentForm.saturation = 130
      environmentForm.hue = 15
      break
    case 'night':
      environmentForm.brightness = 30
      environmentForm.contrast = 120
      environmentForm.saturation = 70
      environmentForm.hue = 240
      break
  }
}

// 应用预设
const applyPreset = (preset: any) => {
  selectedPresetId.value = preset.id
  
  // 应用预设参数
  environmentForm.timeOfDay = preset.timeOfDay
  environmentForm.weather = preset.weather
  environmentForm.brightness = preset.brightness
  environmentForm.contrast = preset.contrast
  environmentForm.saturation = preset.saturation
  
  if (preset.hue !== undefined) {
    environmentForm.hue = preset.hue
  }
  
  message.success(`已应用预设: ${preset.name}`)
}

// 保存环境设置
const saveEnvironment = () => {
  message.success('环境设置已保存')
  // 在实际项目中，这里会调用 API 保存环境数据
}

// 返回上一页
const goBack = () => {
  router.back()
}

// 生命周期钩子
onMounted(() => {
  // 初始化
})
</script>

<style lang="scss" scoped>
.environment-editor-container {
  padding: 20px;
  height: 100%;
  overflow-y: auto;
  
  .environment-card {
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      
      h1 {
        margin: 0;
        font-size: 24px;
        font-weight: 500;
      }
      
      .header-actions {
        display: flex;
        gap: 12px;
      }
    }
  }
  
  .content-layout {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 24px;
    margin-top: 20px;
    
    .preview-section {
      .preview-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 16px;
        
        h3 {
          margin: 0;
        }
      }
      
      .preview-container {
        border: 1px solid var(--border-color);
        border-radius: 4px;
        overflow: hidden;
        
        .environment-preview {
          position: relative;
          height: 225px;
          
          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
          
          &.time-dawn {
            background-color: #f8e8c8;
          }
          
          &.time-day {
            background-color: #f0f8ff;
          }
          
          &.time-dusk {
            background-color: #ffc080;
          }
          
          &.time-night {
            background-color: #101830;
          }
          
          .weather-effect,
          .lighting-effect {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            pointer-events: none;
          }
          
          .weather-rain {
            background-image: linear-gradient(to bottom, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.2) 100%);
            opacity: 0.7;
          }
          
          .weather-snow {
            background-color: rgba(255, 255, 255, 0.2);
          }
          
          .weather-fog {
            background-color: rgba(255, 255, 255, 0.4);
          }
          
          .weather-clouds {
            background-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.1) 0%, rgba(0, 0, 0, 0) 100%);
          }
        }
      }
    }
    
    .settings-section {
      border: 1px solid var(--border-color);
      border-radius: 4px;
      overflow: hidden;
      
      .tab-content {
        padding: 16px;
        
        h3 {
          margin-top: 0;
          margin-bottom: 16px;
          font-size: 16px;
        }
      }
    }
  }
  
  .presets-section {
    margin-top: 24px;
    
    h3 {
      margin-top: 0;
      margin-bottom: 16px;
    }
    
    .preset-item {
      border: 1px solid var(--border-color);
      border-radius: 4px;
      overflow: hidden;
      cursor: pointer;
      transition: transform 0.2s, box-shadow 0.2s;
      
      &.selected {
        border-color: var(--primary-color);
        box-shadow: 0 0 0 2px rgba(var(--primary-color-rgb), 0.2);
      }
      
      .preset-thumbnail {
        height: 80px;
        overflow: hidden;
        position: relative;
        
        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }
      
      span {
        display: block;
        padding: 4px 8px;
        font-size: 12px;
        text-align: center;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      
      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      }
    }
  }
}

/* 响应式调整 */
@media (max-width: 768px) {
  .environment-editor-container {
    padding: 10px;
    
    .content-layout {
      grid-template-columns: 1fr;
      
      .settings-section {
        margin-top: 20px;
      }
    }
  }
}
</style>